package com.silknet.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.pool.ObjectPool;

public class DBConnection {
	private static final String POOLEDCONNECTIONP_REFIX = "pooledConnection.";
	@SuppressWarnings("unused")
	private static final String POOLEDCONNECTIONP_URL = "jdbc:apache:commons:dbcp:"
			+ POOLEDCONNECTIONP_REFIX;

	@SuppressWarnings("unused")
	private static ObjectPool connectionPool;

	private static final AtomicBoolean inited = new AtomicBoolean(false);

	@SuppressWarnings("unused")
	private static Properties fillPropertie(String key, Properties props) {
		Properties ret = new Properties();
		Set<Object> keys = props.keySet();
		for (Object elem : keys) {
			String dbkey = elem.toString();
			if (dbkey.startsWith(key)) {
				String val = props.getProperty(dbkey);
				dbkey = dbkey.substring(key.length() + 1);
				ret.put(dbkey, val);
			}
		}
		return ret;

	}

	private static void initConnections() throws Exception {

		// Class.forName("org.postgresql.Driver");
		// File file = new File("DB.properties");
		// System.out.println("Searching DB Configuration in "
		// + file.getAbsolutePath());
		// Properties props = new Properties();
		// try {
		// props.load(new FileInputStream(file));
		// } catch (Exception ex) {
		// }
		// Set<Object> keys = props.keySet();
		// TreeMap<Object, Properties>
		// propertieMap = new TreeMap<Object, Properties>();
		// for (Object elem : keys) {
		// String dbkey = elem.toString();
		// if (dbkey.startsWith("db.")) {
		// String val = props.getProperty(dbkey);
		// propertieMap.put(val, fillPropertie(val, props));
		// }
		// }
		//
		// keys = propertieMap.keySet();
		// Class.forName("org.apache.commons.dbcp.PoolingDriver");
		// for (Object elem : keys) {
		// String dbkey = elem.toString();
		// props = propertieMap.get(dbkey);
		// String connectURI = props.getProperty("connectURI");
		// connectionPool = new GenericObjectPool(null);
		// ConnectionFactory connectionFactory = new
		// DriverManagerConnectionFactory(
		// connectURI, props);
		// new PoolableConnectionFactory(connectionFactory, connectionPool,
		// null, null, false, true);
		//
		// PoolingDriver driver = (PoolingDriver) DriverManager
		// .getDriver("jdbc:apache:commons:dbcp:");
		// driver.registerPool(POOLEDCONNECTIONP_REFIX + dbkey, connectionPool);
		//
		// }
	}

	public static Connection getConnection(String dbName) throws Exception {
		if (!inited.get()) {
			initConnections();
			inited.set(true);
		}
		// Connection con = DriverManager.getConnection(POOLEDCONNECTIONP_URL
		// + dbName);
		// Connection con = DriverManager.getConnection(
		// "jdbc:postgresql://127.0.0.1:5432/BetterFly", "bfuser",
		// "bfuser");
		// Connection con = DriverManager.getConnection(
		// "jdbc:postgresql://127.0.0.1:5432/osm", "bfuser", "bfuser");
		InitialContext cxt = new InitialContext();
		if (cxt == null) {
			throw new Exception("Uh oh -- no context!");
		}

		DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/OSMDB");

		if (ds == null) {
			throw new Exception("Data source not found!");
		}
		Connection con = ds.getConnection();
		con.setAutoCommit(false);
		return con;
	}
}
